"""
des: PyCurl
author: Mr_52Hz
date: 2020-03-25
"""
import pycurl
from io import BytesIO

# 基本参数
# 请求地址 请求方法 请求头TOKEN
url = 'http://192.168.47.52:8080/user/device/'
method = 'POST'
TOKEN_HEADER = ('TOKEN: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6Imxhb3dhbmciLCJ1dWlkIjoiNTBlMTNjZTktNmI2ZS'
                '00NTBmLWI1NzUtNDg5ZGJiNmUyNjYwIn0.kSr6hwwrltDkBufSZxpKnPVUEZrLJAzh6XFjaO6IB-U',)

# 拼接请求体
body_platform = '1'.ljust(8, '#')
body_user = '50e13ce96b6e450fb575489dbb6e2660'.ljust(36, '#')
body_device = 'devicemsg'.ljust(36, '#')
body_company = 'whaty'.ljust(36, '#')
body_sdk = '3.5.4'.ljust(36, '#')
body_sys_id = 'sysidksdjfasdkf'.ljust(36, '#')
body_book = '8db4b6a424404b408cdb73111ab9b147'.ljust(36, '#')
request_body = body_platform + body_user + body_company + body_device + body_sdk + body_sys_id + body_book

# 用于存储响应体
buffer = BytesIO()

c = pycurl.Curl()
# 配置请求url、method、header、body
c.setopt(c.URL, url)
c.setopt(c.CUSTOMREQUEST, method)
c.setopt(c.HTTPHEADER, TOKEN_HEADER)
c.setopt(c.POSTFIELDS, request_body)

# 响应写入buffer
c.setopt(c.WRITEDATA, buffer)

# 执行请求
c.perform()
c.close()

# 获取响应体数据
body = buffer.getvalue().decode()
print(body)


